package com.sgcc.ln.pmos.e_ln_order.fragment;


import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.launcher.ARouter;
import com.flyco.tablayout.SlidingTabLayout;
import com.sgcc.ln.pmos.arouter.order.OrderArouterPath;
import com.sgcc.ln.pmos.arouter.personalcenter.PersonalCenterArouterPath;
import com.sgcc.ln.pmos.common.adapter.HomeTabPagerAdapter;
import com.sgcc.ln.pmos.common.base.BaseFragment;
import com.sgcc.ln.pmos.common.bean.EventCenter;
import com.sgcc.ln.pmos.common.utils.ArguConstant;
import com.sgcc.ln.pmos.common.utils.UserManager;
import com.sgcc.ln.pmos.common.utils.VisibilConfig;
import com.sgcc.pmos.nativesdk.R;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class OrderFragment extends BaseFragment {

    private SlidingTabLayout mStlContent;
    private ViewPager mVpContent;
    private RelativeLayout mRlSearch;
    private RelativeLayout mRlMessage;
    private List<Fragment> fragments = new ArrayList<>();
    private HomeTabPagerAdapter homeTabPagerAdapter;


    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_order;
    }


    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Override
    public void onEventCallBack(EventCenter eventCenter) {
        super.onEventCallBack(eventCenter);
        switch (eventCenter.getEventCode()) {
            case ArguConstant.GO_CONTRACT:
                Log.e("turbo", "onEventCallBack: ");
                mStlContent.setCurrentTab(1);
                break;
            case ArguConstant.GO_ORDER_LIST:
                Log.e("turbo", "onEventCallBack: ");
                mStlContent.setCurrentTab(0);
                break;
            case ArguConstant.GO_ORDER_LIST_FRFRESH:
                Log.e("turbo", "onEventCallBack: ");
                mStlContent.setCurrentTab(0);
                EventBus.getDefault().post(new EventCenter<>(ArguConstant.REFRESH_INDENT));
                break;
            case ArguConstant.GO_CONTRACT_FRFRESH:
                Log.e("turbo", "onEventCallBack: ");
                mStlContent.setCurrentTab(1);
                EventBus.getDefault().post(new EventCenter<>(ArguConstant.REFRESH_CONTRACT));
                break;


            default:
                break;
        }
    }

    @Override
    protected void initViews() {
        super.initViews();
        mStlContent = (SlidingTabLayout) mRootView.findViewById(R.id.stl_content);
        mVpContent = (ViewPager) mRootView.findViewById(R.id.vp_content);

        mRlSearch = (RelativeLayout) mRootView.findViewById(R.id.rl_search);
        mRlMessage = (RelativeLayout) mRootView.findViewById(R.id.rl_message);
        if(!VisibilConfig.isShowVisibil()){
            mRlMessage.setVisibility(View.INVISIBLE);
        }

        String[] titleList = getResources().getStringArray(R.array.order_title);

        IndentFragment indentFragment = new IndentFragment();
        ContractFragment contractFragment = new ContractFragment();

        fragments.add(indentFragment);
        fragments.add(contractFragment);
        homeTabPagerAdapter = new HomeTabPagerAdapter(getActivity().getSupportFragmentManager(), fragments);
        homeTabPagerAdapter.setmBottomTabNames(titleList);
        mVpContent.setOffscreenPageLimit(2);
        mVpContent.setAdapter(homeTabPagerAdapter);
        mStlContent.setViewPager(mVpContent);

        mVpContent.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mRlSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (UserManager.toLogin()) {
                    ARouter.getInstance()
                            .build(OrderArouterPath.ORDER_SEARCHORER)
                            .withInt("searchType", mVpContent.getCurrentItem())
                            .navigation();
                }



            }
        });
        mRlMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build(PersonalCenterArouterPath.PERSONALCENTER_MESSAGE)
                        .navigation();
            }
        });
    }
}
